<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<md-dialog aria-label="Create a subscription">
  <md-dialog-content layout-padding>
    <h4>Create a subscription</h4>
    <h5>Choose an application</h5>
    <form name="CreateSubscriptionForm" ng-submit="$event.preventDefault()" novalidate>
      <md-autocomplete
        md-selected-item="dialogSubscriptionCreateController.selectedApp"
        md-selected-item-change="dialogSubscriptionCreateController.selectedItemChange()"
        md-search-text="searchText"
        md-items="app in dialogSubscriptionCreateController.searchApplication(searchText)"
        md-item-text="app.name"
        md-min-length="1"
        placeholder="Search application ..."
        md-autofocus="true"
        md-menu-class="autocomplete-create-subscription"
        required
        style="width: 400px"
      >
        <md-item-template>
          <span class="item-title"> <span> {{app.name}} </span><span ng-if="app.type">({{app.type}}) </span> </span>
          <span class="item-metadata"> By {{app.owner.displayName}} on {{app.created_at | humanDateFilter}} </span>
        </md-item-template>
        <md-not-found> No application matching "{{searchText}}" were found. </md-not-found>
      </md-autocomplete>
    </form>

    <h5>Select a plan</h5>
    <form name="CustomApiKeyForm" ng-submit="$event.preventDefault()">
      <div layout="column" layout-align="start start" ng-cloak>
        <span ng-if="dialogSubscriptionCreateController.atLeastOnePlanWithGeneralConditions()" class="general_conditions_disabled"
          >Plan with general conditions can only be subscribed through the portal</span
        >
        <md-radio-group ng-model="dialogSubscriptionCreateController.selectedPlan" style="width: 100%">
          <div ng-repeat="plan in dialogSubscriptionCreateController.plans | orderBy: 'order'">
            <md-radio-button
              value="{{plan.id}}"
              ng-disabled="dialogSubscriptionCreateController.planAlreadyHaveSubscriptions(plan.id) || dialogSubscriptionCreateController.hasGeneralConditions(plan)"
            >
              {{plan.name}}
              <md-tooltip md-direction="right"> {{plan.description}} </md-tooltip>
              <div
                ng-if="dialogSubscriptionCreateController.planAlreadyHaveSubscriptions(plan.id)"
                style="color: rgba(0, 0, 0, 0.54); font-size: smaller"
              >
                There is already a <i>pending</i> or <i>accepted</i> subscription for this plan.
              </div>
            </md-radio-button>

            <div
              ng-if="dialogSubscriptionCreateController.selectedPlan === plan.id && dialogSubscriptionCreateController.planIsApiKey(plan.id) && dialogSubscriptionCreateController.canUseCustomApiKey"
              style="color: rgba(0, 0, 0, 0.54); font-size: smaller; margin: 0 0 0 30px"
            >
              <api-key-validated-input
                form-reference="CustomApiKeyForm"
                api-id="dialogSubscriptionCreateController.api.id"
                application-id="dialogSubscriptionCreateController.selectedApp.id"
                label="'You can provide a custom API key if you already have one'"
                on-change="dialogSubscriptionCreateController.onApiKeyValueChange"
              ></api-key-validated-input>
            </div>
          </div>
        </md-radio-group>
      </div>
    </form>
  </md-dialog-content>

  <md-dialog-actions layout="row">
    <md-button ng-click="dialogSubscriptionCreateController.hide()"> Cancel </md-button>
    <md-button
      class="md-raised md-primary"
      ng-click="dialogSubscriptionCreateController.save()"
      ng-disabled="!dialogSubscriptionCreateController.selectedApp
                            || !dialogSubscriptionCreateController.selectedPlan
                            || dialogSubscriptionCreateController.customApiKeyInputState === 'invalid'
                            || CustomApiKeyForm.$invalid
    "
    >
      Create
    </md-button>
  </md-dialog-actions>
</md-dialog>
